package com.partterns.singleton;

/**
 * 创建一种安全的单例模式，防止被反射攻击
 */
public class SafeSingleton {
    private static boolean flag=false;
    private SafeSingleton(){
        synchronized (SafeSingleton.class)
        {
            if(!flag){
                flag=true;
            }else{
                throw new RuntimeException("fuck,单例模式被侵犯");
            }
        }

    }
    private static class SafeSingletonHolder{
        private static final SafeSingleton INSTANCE=new SafeSingleton();
    }
    public static SafeSingleton getInstance(){
        return SafeSingletonHolder.INSTANCE;
    }
    private void saySomeThing(){
        System.out.println("嘻嘻嘻");
    }
}
